package controller;

import model.SC;
import service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@Controller
public class ScoreController{
    private ScoreService scoreService;

    public void setScoreService(ScoreService scoreService){
        this.scoreService=scoreService;
    }

    @RequestMapping("/getAll_score")
    public ModelAndView getAll() throws Exception{
        ArrayList<SC> score=scoreService.getAllScores();
        ModelAndView modelAndView=new ModelAndView("show_grade");
        modelAndView.addObject("Scores",score);
        return modelAndView;
    }

    @RequestMapping("/delete_score")
    public String delete(HttpServletRequest request, HttpServletResponse response,
                         RedirectAttributes redirectAttributes) throws Exception{
        String cno= request.getParameter("cno");
        String sno= request.getParameter("sno");
        boolean success=scoreService.deleteScore(cno,sno);
        if(success){
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
            return "redirect:/show_grade.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
            return "redirect:/show_grade.jsp";
        }

    }

    @RequestMapping("/get_score")
    public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String cno= request.getParameter("cno");
        String sno= request.getParameter("sno");
        ModelAndView modelAndView=new ModelAndView("show_grade1");
        ArrayList<SC> score;
        if(cno!=null && sno!=null){//按学号和课程号查询
            score=scoreService.getScore(cno,sno);

        } else if(cno ==null){//按学号查询
            score=scoreService.getScore_sno(sno);

        } else{//按课程号查询
            score=scoreService.getScore_cno(cno);

        }
        modelAndView.addObject("score",score);
        return modelAndView;
    }

    @RequestMapping("/edit_score")
    public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String cno= request.getParameter("cno");
        String sno= request.getParameter("sno");
        ArrayList<SC> score=scoreService.getScore(cno,sno);
        ModelAndView modelAndView=new ModelAndView("show_grade");
        modelAndView.addObject("Score",score);
        return modelAndView;
    }

    @RequestMapping("/doEdit_score")
    public String doEdit(HttpServletRequest request,HttpServletResponse response,double pscj,
                         double kscj,double grade,RedirectAttributes redirectAttributes) throws Exception{
        String cno= request.getParameter("cno");
        String sno= request.getParameter("sno");
        SC score=new SC();
        score.setCno(cno);
        score.setSno(sno);
        score.setPscj(pscj);
        score.setKscj(kscj);
        score.setGrade(grade);
        boolean succ=scoreService.editScore(score);
        if(succ){
            redirectAttributes.addFlashAttribute("succ", "修改成功！");
            return "redirect:/show_grade.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "修改失败！");
            return "redirect:/show_grade.jsp";
        }

    }

    @RequestMapping("/add_score")
    public ModelAndView add() throws Exception{
        ModelAndView modelAndView=new ModelAndView("show_grade");
        return modelAndView;

    }

    @RequestMapping("/doAdd_score")
    public String doAdd(String cno,String sno,double pscj,double kscj,double grade,
                        RedirectAttributes redirectAttributes)throws Exception{
        SC score=new SC();
        score.setCno(cno);
        score.setSno(sno);
        score.setPscj(pscj);
        score.setKscj(kscj);
        score.setGrade(grade);
        boolean succ=scoreService.addScore(score);
        if (succ){
            redirectAttributes.addFlashAttribute("succ", "添加成功！");
            return "redirect:/show_grade.jsp";
        }else {
            redirectAttributes.addFlashAttribute("succ", "添加失败！");
            return "redirect:/show_grade.jsp";
        }
    }
}
